package com.ciwong.epaper.modules.cordva.html;

import android.content.Intent;
import android.util.Log;

import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.cordva.BaseHtmlActicity;
import com.ciwong.epaper.modules.cordva.SubmitEvent;
import com.ciwong.epaper.modules.epaper.bean.CatalogueInfo;
import com.ciwong.epaper.modules.epaper.bean.Module;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.download.DownLoadInfo;
import com.ciwong.libs.utils.CWLog;
import com.ciwong.libs.utils.volley.BaseRequest;

import org.json.JSONObject;

/**
 * Created by Administrator on 2017/6/16.
 * 时文在线
 */

public class TimeNewsActivity extends BaseHtmlActicity {

    private String clientId;
    private String versionId;

    private DownLoadInfo downLoadInfo;
    private Module mModule;
    private int position;

    @Override
    protected void preCreate() {
        super.preCreate();

        Intent intent = getIntent();

        if (null != intent) {

            mModule = (Module) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_OBJ);
            downLoadInfo = (DownLoadInfo) intent.getSerializableExtra(IntentFlag.INTENT_FLAG_DOWNLOAD_INFO);
            position = intent.getIntExtra(IntentFlag.INTENT_FLAG_POSITION, 0);

            String title = "";

//
            if (downLoadInfo.getType() == CatalogueInfo.PackageMode.PACKAGE_MODE_NEW) {//新资源
                versionId = downLoadInfo.getVersionId();
                title = downLoadInfo.getResourceName();

            } else {//旧资源
                versionId = mModule.getResourceList().get(position).getVersionId();
                title = mModule.getResourceList().get(position).getResourceName();
            }

//            setTitleText(title);
        }

    }

    @Override
    protected void init() {
        super.init();

    }


    @Override
    protected void loadData() {
        super.loadData();
        BaseRequest.VerifyInfo verifyInfo = HttpRequest.getVerifyInfo();
        if (verifyInfo != null) {
            clientId = verifyInfo.getClientId();
        }
        Log.d("xixi", "时文 versionId" + versionId);
        String url = EpaperConstant.URL_H5_TIME_NEWS + "versionId=" + versionId + "&brandId=" + EApplication.BRAND_ID + "&clientId=" + clientId;
        setStartURL(url);
        CWLog.i("时文url:", url);

        loadUrl(startURL.toString());

    }


    public void onEventMainThread(SubmitEvent submitEvent) {
        Log.d(TAG, "onEventMainThread+" + submitEvent.getId() + "--Json:" + submitEvent.getJson());

        if (submitEvent.getId().equals("SetTitle")) {
            try {
                String arg = submitEvent.getJson();
                JSONObject jsonObject = new JSONObject(arg);
                if (jsonObject.has("title")) {
                    String currenttitle = jsonObject.getString("title");
                    setTitleText(currenttitle);
                }
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
    }


}
